package com.sphd.restaurantsystem.model;


import com.android.volley.VolleyError;
import com.sphd.restaurantsystem.info.PushMoneyInfo;
import com.sphd.restaurantsystem.info.PushScaleInfo;
import com.sphd.restaurantsystem.interfaces.VolleyJsonObjectCallBack;
import com.sphd.restaurantsystem.presenter.PushMoneyPresenterImpl;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Hu on 2016/12/12
 */

public class PushMoneyModelImpl implements PushMoneyModel, VolleyJsonObjectCallBack {
    PushMoneyPresenterImpl pushMoneyPresenter;

    @Override
    public void sendUrl(String url, PushMoneyPresenterImpl pushMoneyPresenter) {
        this.pushMoneyPresenter = pushMoneyPresenter;
        volleyUtil.setJsonObjectRequest(url, this);
    }

    @Override
    public void onResponse(JSONObject response) {
        List<PushMoneyInfo> pushMoneyInfos = new ArrayList<>();
        getList(response, "listtoday", pushMoneyInfos);
        getList(response, "listthismonth", pushMoneyInfos);
        getList(response, "listlastmonth", pushMoneyInfos);

        try {
            List<PushScaleInfo> scaleInfos = JsonUtil.getListFromString(response.getString("list"), PushScaleInfo.class);
            double daytopup = response.getDouble("daytopup");
            double dayorder0 = response.getDouble("dayorder0");
            double dayorder1 = response.getDouble("dayorder1");

            pushMoneyPresenter.sendRes(pushMoneyInfos,scaleInfos,daytopup,dayorder0,dayorder1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        pushMoneyPresenter.sendError(error);
    }

    private void getList(JSONObject jsonObject, String key, List<PushMoneyInfo> pushMoneyInfos) {
        if (jsonObject.has(key)) {
            try {
                JSONArray jsonArray = jsonObject.getJSONArray(key);
                for (int i = 0; i < jsonArray.length(); i++) {
                    PushMoneyInfo pushInfo = new PushMoneyInfo();
                    JSONObject object = jsonArray.getJSONObject(i);
                    if (object.has("IFNULL(sum(salary_sum),0)")) {
                        pushInfo.setSalary_sum(object.getDouble("IFNULL(sum(salary_sum),0)"));
                    }
                    if (object.has("IFNULL(SUM(salary_discount),0)")) {
                        pushInfo.setSalary_discount(object.getDouble("IFNULL(SUM(salary_discount),0)"));
                    }
                    if (object.has("IFNULL(SUM(salary_common),0)")) {
                        pushInfo.setSalary_common(object.getDouble("IFNULL(SUM(salary_common),0)"));
                    }
                    if (object.has("IFNULL(sum(salary_topup),0)")) {
                        pushInfo.setSalary_topup(object.getDouble("IFNULL(sum(salary_topup),0)"));
                    }
                    pushMoneyInfos.add(pushInfo);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}